package com.yanqu.road.dao.impl.activity.shuihucard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shuihu.ShuiHuBoxItem;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ShuiHuBoxItemDaoImpl extends TempDataObjectDao<ShuiHuBoxItem> {

    public static String getShuiHuBoxMaxRoundNoSql = "select max(roundNo) as maxRoundNo from t_c_activity_shuihu_box_item where activityId=?";

    public Integer getShuiHuBoxMaxRoundNo(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getShuiHuBoxMaxRoundNoSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return rs.getInt("maxRoundNo");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getShuiHuBoxItemSql = "select * from t_c_activity_shuihu_box_item where activityId=? and roundNo=?";

    public Map<Integer, ShuiHuBoxItem> getShuiHuBoxItemMap(int activityId, int roundNo) {
        Map<Integer, ShuiHuBoxItem> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getShuiHuBoxItemSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, roundNo);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuBoxItem parse = parseData(rs);
                map.put(parse.getItemNo(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateShuiHuBoxItemSql = "update t_c_activity_shuihu_box_item set cardId=?,userId=?,userBaseInfo=? where activityId=? and roundNo=? and itemNo=?";

    @Override
    protected boolean updateData(ShuiHuBoxItem shuiHuBoxItem) {
        shuiHuBoxItem.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuBoxItem.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuBoxItem.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getRoundNo()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getItemNo()));
        if (getDb().executeNoneQuery(updateShuiHuBoxItemSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuiHuBoxItemSql = "insert into t_c_activity_shuihu_box_item(activityId,roundNo,itemNo,cardId,userId,userBaseInfo) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(ShuiHuBoxItem shuiHuBoxItem) {
        shuiHuBoxItem.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getRoundNo()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getItemNo()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuBoxItem.getCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuBoxItem.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuBoxItem.getUserBaseInfo())));
        if (getDb().executeNoneQuery(insertShuiHuBoxItemSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ShuiHuBoxItem parseData(ResultSet rs) throws SQLException {
        ShuiHuBoxItem shuiHuBoxItem = new ShuiHuBoxItem();
        shuiHuBoxItem.setActivityId(rs.getInt("activityId"));
        shuiHuBoxItem.setRoundNo(rs.getInt("roundNo"));
        shuiHuBoxItem.setItemNo(rs.getInt("itemNo"));
        shuiHuBoxItem.setCardId(rs.getInt("cardId"));
        shuiHuBoxItem.setUserId(rs.getLong("userId"));
        String userBaseInfo = rs.getString("userBaseInfo");
        shuiHuBoxItem.setUserBaseInfo(StringUtils.isNullOrEmpty(userBaseInfo) ? new UserBaseInfo() : JSONObject.parseObject(userBaseInfo, new TypeReference<UserBaseInfo>() {
        }));
        return shuiHuBoxItem;
    }
}
